﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit.RevitExtension;
using YJKRevitPreview;
using YJKGeometryFuncs;

namespace YJKRevitBalc
{
	public partial class CreateBalconyForm : System.Windows.Forms.Form
	{
		public CreateBalconyForm()
		{
			this.m_viewBalcony = new Previewer(1u);
			this.m_viewBalcony.Paint += this.viewBalcony_Paint;
			base.Controls.Add(this.m_viewBalcony);
			this.m_viewBalcony.Location = new System.Drawing.Point(198, 19);
			this.m_viewBalcony.Size = new Size(165, 130);
			this.m_viewBalcony.TabIndex = 18;
			this.m_viewBalcony.TabStop = false;
			this.m_viewBalcony.Enabled = false;
			this.InitializeComponent();
			new ToolTip().SetToolTip(this.btnDrawBalcony1, "创建普通阳台");
			new ToolTip().SetToolTip(this.btnDrawBalcony2, "在凹墙创建阳台");
			new ToolTip().SetToolTip(this.btnDrawBalcony3, "在墙角创建阳台");
			new ToolTip().SetToolTip(this.btnDrawBalcony4, "沿墙创建阳台");
			new ToolTip().SetToolTip(this.btnDrawBalcony5, "自由绘制阳台");
			new ToolTip().SetToolTip(this.btnDrawBalcony6, "拾取路径绘制阳台");
		}

		private void CreateBalconyForm_Load(object sender, EventArgs e)
		{
			this.cmbWallType.Items.Clear();
			this.cmbFloorType.Items.Clear();
			Helper.AddCombItems(IListDate.WallTypeList, this.cmbWallType);
			Helper.AddCombItems(IListDate.FloorTypeList, this.cmbFloorType);
			if (this.m_bEditBalcony)
			{
				this.m_bPickBalconyWidth = false;
				this.btnDrawBalcony1.Visible = false;
				this.btnDrawBalcony2.Visible = false;
				this.btnDrawBalcony3.Visible = false;
				this.btnDrawBalcony4.Visible = false;
				this.btnDrawBalcony5.Visible = false;
				this.btnDrawBalcony6.Visible = false;
				this.radioBalconyWidth2.Enabled = false;
				this.Text = "编辑阳台";
			}
			else
			{
				this.ReadDlgData();
				this.btnOK.Visible = false;
				this.btnCancel.Visible = false;
			}
			this.SetEnableDlgCtrl();
			this.UpdateDlgData(false);
			this.RefreshPreview(-1);
		}

		private void viewBalcony_Paint(object sender, PaintEventArgs e)
		{
			if (this.mIsPaint)
			{
				this.mIsPaint = false;
				e.Graphics.Clear(System.Drawing.Color.White);
				this.RefreshPreview();
			}
		}

		private void RefreshPreview(int nDrawDimType)
		{
			this.mIsPaint = true;
			this.mDrawDimType = nDrawDimType;
			this.m_viewBalcony.Refresh();
		}

		private void RefreshPreview()
		{
			List<TextNoteInfo> list = new List<TextNoteInfo>();
			List<CurveInfo> list2 = new List<CurveInfo>();
			try
			{
				double num = 150.0;
				double dTextH = 80.0;
				double num2 = 100.0;
				double num3 = 50.0;
				double num4 = 700.0;
				double num5 = 800.0;
				double num6 = 600.0;
				Line curve = YJKLineEx.YJKGetBound(new XYZ(0.0, num4 + num, 0.0), new XYZ(0.0, -num3, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Gray, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(0.0, -2.0 * num3, 0.0), new XYZ(0.0, -num, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Gray, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(num2, num4 + num, 0.0), new XYZ(num2, 0.0, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Gray, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(num2, -num3, 0.0), new XYZ(num2, -num, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Gray, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(num2, 0.0, 0.0), new XYZ(num2 + 2.0 * num, 0.0, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Gray, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(num2, -num3, 0.0), new XYZ(num2 + 2.0 * num, -num3, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Gray, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(0.0, 0.0, 0.0), new XYZ(num2, 0.0, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Gray, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(0.0, num4, 0.0), new XYZ(num2, num4, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Gray, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(0.5 * num2 - 10.0, 0.0, 0.0), new XYZ(0.5 * num2 - 10.0, num4, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Gray, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(0.5 * num2 + 10.0, 0.0, 0.0), new XYZ(0.5 * num2 + 10.0, num4, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Gray, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(0.0, -num3, 0.0), new XYZ(-(num5 - num3), -num3, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Red, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(0.0, -2.0 * num3, 0.0), new XYZ(-num5, -2.0 * num3, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Red, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(-(num5 - num3), -num3, 0.0), new XYZ(-(num5 - num3), num6 - 2.0 * num3, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Red, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(-num5, -2.0 * num3, 0.0), new XYZ(-num5, num6 - 2.0 * num3, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Red, 1f));
				curve = YJKLineEx.YJKGetBound(new XYZ(-num5, num6 - 2.0 * num3, 0.0), new XYZ(0.0, num6 - 2.0 * num3, 0.0));
				list2.Add(new CurveInfo(curve, PenColor.PC_Red, 1f));
				if (this.mDrawDimType == 0)
				{
					XYZ ptBase = new XYZ(-num5, num6 - 2.0 * num3, 0.0);
					XYZ ptBase2 = new XYZ(0.0, num6 - 2.0 * num3, 0.0);
					this.DrawDim(ptBase, ptBase2, dTextH, "阳台宽度", list, list2);
				}
				else if (this.mDrawDimType == 1)
				{
					XYZ ptBase3 = new XYZ(0.0, -num3, 0.0);
					XYZ ptBase4 = new XYZ(0.0, num6 - 2.0 * num3, 0.0);
					this.DrawDim(ptBase3, ptBase4, dTextH, "栏板高度", list, list2);
				}
				else if (this.mDrawDimType == 2)
				{
					XYZ ptBase5 = new XYZ(0.0, -num3, 0.0);
					XYZ ptBase6 = new XYZ(0.0, 0.0, 0.0);
					this.DrawDim(ptBase5, ptBase6, dTextH, "地面标高", list, list2);
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
			}
			this.m_viewBalcony.TextNotes = list;
			this.m_viewBalcony.Curves = list2;
			this.m_viewBalcony.ZoomExtents();
		}

		private void DrawDim(XYZ ptBase1, XYZ ptBase2, double dTextH, string sDimText, List<TextNoteInfo> lstText, List<CurveInfo> lstLine)
		{
			XYZ xyz = (ptBase2 - ptBase1).Normalize();
			XYZ xyz2 = Geometry.RotateTo(xyz, Math.PI*.5, XYZ.BasisZ);
			Line curve = YJKLineEx.YJKGetBound(ptBase1, ptBase1 + xyz2 * 2.0 * dTextH);
			lstLine.Add(new CurveInfo(curve, PenColor.PC_Blue, 1f));
			curve = YJKLineEx.YJKGetBound(ptBase2, ptBase2 + xyz2 * 2.0 * dTextH);
			lstLine.Add(new CurveInfo(curve, PenColor.PC_Blue, 1f));
			ptBase1 += xyz2 * 1.5 * dTextH;
			ptBase2 += xyz2 * 1.5 * dTextH;
			XYZ xyz3 = Geometry.RotateTo(xyz, 0.78539816339744828, XYZ.BasisZ);
			curve = YJKLineEx.YJKGetBound(ptBase1 + xyz3 * 0.5 * dTextH, ptBase1 - xyz3 * 0.5 * dTextH);
			lstLine.Add(new CurveInfo(curve, PenColor.PC_Blue, 1f));
			curve = YJKLineEx.YJKGetBound(ptBase2 + xyz3 * 0.5 * dTextH, ptBase2 - xyz3 * 0.5 * dTextH);
			lstLine.Add(new CurveInfo(curve, PenColor.PC_Blue, 1f));
			curve = YJKLineEx.YJKGetBound(ptBase1, ptBase2);
			lstLine.Add(new CurveInfo(curve, PenColor.PC_Blue, 1f));
			double angle = xyz.AngleTo(XYZ.BasisX);
			double num = ((double)sDimText.Length + 2.2) * dTextH;
			XYZ xyz4;
			if (num < ptBase1.DistanceTo(ptBase2))
			{
				xyz4 = ptBase1 + (ptBase2 - ptBase1) / 2.0;
			}
			else
			{
				xyz4 = ptBase2 + xyz * num / 2.0;
			}
			xyz4 += xyz2 * 1.2 * dTextH;
			TextNoteInfo item = new TextNoteInfo(sDimText, PenColor.PC_Blue, dTextH, xyz4, num, 1.5 * dTextH, angle);
			lstText.Add(item);
		}

		private void SaveDlgData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "BalconySet.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.m_dBalconyWidth);
			streamWriter.WriteLine(this.m_dWallHeight);
			streamWriter.WriteLine(this.m_dLevelElevation);
			streamWriter.WriteLine(this.m_sWallType);
			streamWriter.WriteLine(this.m_sFloorType);
			streamWriter.WriteLine(this.m_bPickBalconyWidth);
			streamWriter.WriteLine(this.cmbWallType.SelectedIndex);
			streamWriter.WriteLine(this.cmbFloorType.SelectedIndex);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadDlgData()
		{
			string path = Path.GetTempPath() + "BalconySet.dat";
			if (!File.Exists(path))
			{
				return true;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				this.m_dBalconyWidth = Convert.ToDouble(streamReader.ReadLine());
				this.m_dWallHeight = Convert.ToDouble(streamReader.ReadLine());
				this.m_dLevelElevation = Convert.ToDouble(streamReader.ReadLine());
				this.m_sWallType = streamReader.ReadLine();
				this.m_sFloorType = streamReader.ReadLine();
				this.m_bPickBalconyWidth = Convert.ToBoolean(streamReader.ReadLine());
				int num = Convert.ToInt32(streamReader.ReadLine());
				if (num > -1 && num < this.cmbWallType.Items.Count)
				{
					this.cmbWallType.SelectedIndex = num;
				}
				num = Convert.ToInt32(streamReader.ReadLine());
				if (num > -1 && num < this.cmbFloorType.Items.Count)
				{
					this.cmbFloorType.SelectedIndex = num;
				}
			}
			catch
			{
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		private void UpdateDlgData(bool bGetDlgData)
		{
			if (bGetDlgData)
			{
				this.m_dBalconyWidth = Convert.ToDouble(this.txtDistince.Text);
				this.m_dWallHeight = Convert.ToDouble(this.txtWallHeight.Text);
				this.m_dLevelElevation = Convert.ToDouble(this.txtLevel.Text);
				this.m_sWallType = this.cmbWallType.Text;
				this.m_sFloorType = this.cmbFloorType.Text;
				this.m_bPickBalconyWidth = this.radioBalconyWidth2.Checked;
				return;
			}
			this.txtDistince.Text = this.m_dBalconyWidth.ToString("0");
			this.txtWallHeight.Text = this.m_dWallHeight.ToString("0");
			this.txtLevel.Text = this.m_dLevelElevation.ToString("0");
			int num = this.cmbWallType.FindString(this.m_sWallType);
			if (num >= 0)
			{
				this.cmbWallType.SelectedIndex = num;
			}
			else
			{
				this.cmbWallType.SelectedIndex = 0;
			}
			num = this.cmbFloorType.FindString(this.m_sFloorType);
			if (num >= 0)
			{
				this.cmbFloorType.SelectedIndex = num;
			}
			else
			{
				this.cmbFloorType.SelectedIndex = 0;
			}
			this.radioBalconyWidth1.Checked = !this.m_bPickBalconyWidth;
			this.radioBalconyWidth2.Checked = this.m_bPickBalconyWidth;
		}

		private void btnDrawBalcony1_Click(object sender, EventArgs e)
		{
			this.m_BalconyType = CreateBalconyForm.BalconyType.RT_Normal;
			this.DlgOnOk();
		}

		private void btnDrawBalcony2_Click(object sender, EventArgs e)
		{
			this.m_BalconyType = CreateBalconyForm.BalconyType.RT_NoBorder;
			this.DlgOnOk();
		}

		private void btnDrawBalcony3_Click(object sender, EventArgs e)
		{
			this.m_BalconyType = CreateBalconyForm.BalconyType.RT_Corner;
			this.DlgOnOk();
		}

		private void btnDrawBalcony4_Click(object sender, EventArgs e)
		{
			this.m_BalconyType = CreateBalconyForm.BalconyType.RT_AlongWall;
			this.DlgOnOk();
		}

		private void btnDrawBalcony5_Click(object sender, EventArgs e)
		{
			this.m_BalconyType = CreateBalconyForm.BalconyType.RT_DrawBaseLine;
			this.DlgOnOk();
		}

		private void btnDrawBalcony6_Click(object sender, EventArgs e)
		{
			this.m_BalconyType = CreateBalconyForm.BalconyType.RT_PickBaseLine;
			this.DlgOnOk();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.DlgOnOk();
		}

		private void radioBalconyWidth1_Click(object sender, EventArgs e)
		{
			this.m_bPickBalconyWidth = false;
			this.SetEnableDlgCtrl();
		}

		private void radioBalconyWidth2_Click(object sender, EventArgs e)
		{
			this.m_bPickBalconyWidth = true;
			this.SetEnableDlgCtrl();
		}

		private void SetEnableDlgCtrl()
		{
			this.txtDistince.Enabled = !this.m_bPickBalconyWidth;
		}

		public void DlgOnOk()
		{
			this.UpdateDlgData(true);
			if (!this.m_bEditBalcony)
			{
				this.SaveDlgData();
			}
			base.DialogResult = DialogResult.OK;
		}

		private void txtWallHeight_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				if (double.Parse(this.txtWallHeight.Text) < 100.0)
				{
					YJKMessageBox.Show("栏板高度最低为“100”");
					e.Cancel = true;
				}
			}
			catch
			{
				YJKMessageBox.Show("栏板高度输入值无效.");
				e.Cancel = true;
			}
		}

		private void txtLevel_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double.Parse(this.txtLevel.Text);
			}
			catch
			{
				YJKMessageBox.Show("地面标高输入值无效.");
				e.Cancel = true;
			}
		}

		private void txtDistince_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				if (double.Parse(this.txtDistince.Text) < 500.0)
				{
					YJKMessageBox.Show("阳台宽度最少为500");
					e.Cancel = true;
				}
			}
			catch
			{
				YJKMessageBox.Show("阳台宽度输入值无效.");
				e.Cancel = true;
			}
		}

		private void txtWallHeight_Enter(object sender, EventArgs e)
		{
			this.RefreshPreview(1);
		}

		private void txtWallHeight_Leave(object sender, EventArgs e)
		{
			this.RefreshPreview(-1);
		}

		private void txtLevel_Enter(object sender, EventArgs e)
		{
			this.RefreshPreview(2);
		}

		private void txtLevel_Leave(object sender, EventArgs e)
		{
			this.RefreshPreview(-1);
		}

		private void txtDistince_Enter(object sender, EventArgs e)
		{
			this.RefreshPreview(0);
		}

		private void txtDistince_Leave(object sender, EventArgs e)
		{
			this.RefreshPreview(-1);
		}

		private void CreateBalconyForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		public bool m_bEditBalcony;

		public bool m_bPickBalconyWidth;

		public double m_dBalconyWidth = 2000.0;

		public string m_sWallType;

		public double m_dWallHeight = 1200.0;

		public double m_dLevelElevation = -50.0;

		public string m_sFloorType;

		public CreateBalconyForm.BalconyType m_BalconyType;

		private Previewer m_viewBalcony;

		private bool mIsPaint = true;

		private int mDrawDimType = -1;

		public enum BalconyType
		{
			RT_Normal,
			RT_NoBorder,
			RT_Corner,
			RT_AlongWall,
			RT_DrawBaseLine,
			RT_PickBaseLine
		}
	}
}
